﻿using System;
using System.ComponentModel;
using System.Web.Mvc;
using FsCarInfo.Core.Entity;
using FsCarInfo.Facade;
using HMIS.FrameWork;
using HMIS.Infrastructure.Facade.Authority;
using HMIS.MVC.CommonSupport;
using HMIS.MVC.ViewModel;
using Iesi.Collections.Generic;

namespace HMIS.MVC.Controllers
{
	public class CarTransferController : BaseController
	{
		private readonly ICarTransferFacade CarTransferFacade;
		private readonly ICarFacade CarFacade;
		private readonly IHospUserFacade HospUserFacade;

		public CarTransferController(ICarTransferFacade CarTransferFacade, ICarFacade CarFacade, IHospUserFacade HospUserFacade)
		{
			this.CarTransferFacade = CarTransferFacade;
			this.CarFacade = CarFacade;
			this.HospUserFacade = HospUserFacade;
		}

		[Description("访问商品车调拨管理页面")]
		[ViewPage]
		public ActionResult CarTransfer()
		{
			return View();
		}

		[ExtResult]
		[Description("添加、修改商品车调拨单")]
		public ActionResult SaveOrUpdate()
		{
			CarTransfer entity;
			if (String.IsNullOrEmpty(Request["Id"]))
			{
				entity = new CarTransfer
				{
					ManagerUser = Request["ManagerUser"],
					ManagerUserID = Request["ManagerUserID"],
					ApproverUser = Request["ApproverUser"],
					ApproverUserID = Request["ApproverUserID"],
					OutDp = Request["OutDp"],
					OutDpID = Request["OutDpID"],
					InDp = Request["InDp"],
					InDpID = Request["InDpID"],
					OutDpmgUser = Request["OutDpmgUser"],
					OutDpmgUserID = Request["OutDpmgUserID"],
					InDpmgUser = Request["InDpmgUser"],
					InDpmgUserID = Request["InDpmgUserID"],
					ExpOutTime = Convert.ToDateTime(Request["ExpOutTime"]),
					ExpInTime = Convert.ToDateTime(Request["ExpInTime"]),
					Description = Request["DESCRIPTION"],
					Cars = new HashedSet<Car>()
				};

				if (!String.IsNullOrEmpty(Request["Cars"]))
				{
					string[] cars = Request["Cars"].Split(',');
					for (int i = 0; i < cars.Length; i++)
					{
						entity.Cars.Add(CarFacade.Load(cars[i]));
					}
				}
			}
			else
			{
				entity = CarTransferFacade.Load(Request["Id"]);
				entity.Description = Request["DESCRIPTION"];
			}

			// Carstransfer

			CarTransferFacade.SaveOrUpdate(entity);
			return this.JsonFormat(new ExtResult { success = true, id = entity.Id });
		}

		[Description("获取商品车调拨单列表")]
		public ActionResult QueryPlist()
		{
			long total;
			var data = CarTransferFacade.QueryPlist(
				//Request["query"],
				"53",
				int.Parse(Request["start"] == null ? "0" : Request["start"]),
				int.Parse(Request["limit"] == null ? "0" : Request["limit"]),
				Request["sort"],
				Request["dir"],
				this.DesDataFilter(Request["filter"]),
				out total);

			return this.JsonFormat(
				new ExtGirdData<CarTransfer> { total = total, data = data },
				new string[] { "CustomType" });
		}

		[Description("获取商品车调拨单信息")]
		public ActionResult GetById()
		{
			var entity = CarTransferFacade.Get(Request["Id"]);
			return this.JsonFormat(new ExtFormLoadData<CarTransfer> { data = new CarTransfer[] { entity } });
		}
	}
}
